﻿using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.Detailed;
using XbsWebAPI.EntityFrameworkCore.Entities.FeedBack;
using XbsWebAPI.EntityFrameworkCore.Entities.RoomStatus;
using XbsWebAPI.EntityFrameworkCore.Entities.SystemAdmin;

namespace XbsWebAPI.EntityFrameworkCore
{
    public class DataContext : IdentityDbContext<XbsAdminUser>
    {
        public DataContext(DbContextOptions<DataContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Camp>()
                .HasMany(a => a.HouseTypes)
                .WithOne(a => a.Camp)
                .HasForeignKey(a => a.CampId)
                .OnDelete(DeleteBehavior.SetNull);

            modelBuilder.Entity<Camp>()
                .HasMany(a => a.CampImages)
                .WithOne(a => a.Camp)
                .HasForeignKey(a => a.CampId)
                .OnDelete(DeleteBehavior.SetNull);

            #region 房型 一对多 关系

            //房间
            modelBuilder.Entity<HouseType>()
                .HasMany(a => a.Rooms)
                .WithOne(a => a.HouseType)
                .HasForeignKey(a => a.HouseTypeId)
                .OnDelete(DeleteBehavior.SetNull);

            //节假日
            modelBuilder.Entity<HouseType>()
                .HasMany(a => a.HouseTypeHolidays)
                .WithOne(a => a.HouseType)
                .HasForeignKey(a => a.HouseTypeId)
                .OnDelete(DeleteBehavior.SetNull);

            //特殊日
            modelBuilder.Entity<HouseType>()
                .HasMany(a => a.HouseTypeSpecialDays)
                .WithOne(a => a.HouseType)
                .HasForeignKey(a => a.HouseTypeId)
                .OnDelete(DeleteBehavior.SetNull);

            //房型图片
            modelBuilder.Entity<HouseType>()
                .HasMany(a => a.HouseTypePictures)
                .WithOne(a => a.HouseType)
                .HasForeignKey(a => a.HouseTypeId)
                .OnDelete(DeleteBehavior.SetNull);

            #endregion

            #region 房间和房客 一对多关系
            //    modelBuilder.Entity<Room>()
            //.HasMany(a => a.Guests)
            //.WithOne(a => a.Room)
            //.HasForeignKey(a => a.RoomId)
            //.OnDelete(DeleteBehavior.SetNull);

            modelBuilder.Entity<CheckIn>()
                .HasMany(a => a.GuestRecords)
                .WithOne(a => a.CheckIn)
                .HasForeignKey(a => a.CheckInId)
                .OnDelete(DeleteBehavior.SetNull);

            #endregion

            #region 房型与配套设施 多对多

            modelBuilder.Entity<HouseTypeSupportingFacility>()
                .HasKey(x => new { x.HouseTypeId, x.SupportingFacilityId });

            modelBuilder.Entity<HouseTypeSupportingFacility>()
                .HasOne(x => x.HouseType)
                .WithMany(x => x.HouseTypeSupportingFacilities)
                .HasForeignKey(x => x.HouseTypeId);

            modelBuilder.Entity<HouseTypeSupportingFacility>()
                .HasOne(x => x.SupportingFacility)
                .WithMany(x => x.HouseTypeSupportingFacilities)
                .HasForeignKey(x => x.SupportingFacilityId);


            #endregion


        }



        #region 营位房型管理

        public DbSet<Camp> Camps { get; set; }

        public DbSet<CampImage> CampImages { get; set; }

        public DbSet<HouseType> HouseTypes { get; set; }
        public DbSet<HouseTypeHoliday> HouseTypeHolidays { get; set; }
        public DbSet<HouseTypePicture> HouseTypePictures { get; set; }
        public DbSet<HouseTypeSpecialDay> HouseTypeSpecialDays { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<SupportingFacility> SupportingFacilities { get; set; }

        public DbSet<HouseTypeSupportingFacility> HouseTypeSupportingFacilities { get; set; }

        #endregion


        #region 入住\离店管理

        public DbSet<CheckIn> CheckIns { get; set; }

        public DbSet<Guest> Guests { get; set; }

        public DbSet<GuestRecord> GuestRecords { get; set; }

        #endregion


        #region 订单管理

        public DbSet<OrderDetail> OrderDetails { get; set; }

        #endregion

        #region 微信小程序

        public DbSet<MiniProgram> MiniPrograms { get; set; }

        public DbSet<MiniProgramImage> MiniProgramImages { get; set; }

        #endregion

        #region 系统管理 账户 角色 菜单 权限

        public DbSet<Menu> Menus { get; set; }

        

        public DbSet<SystemRole> SystemRoles { get; set; }

        #endregion

        #region 意见反馈
        public DbSet<FeedBack> FeedBacks { get; set; }

        #endregion

        #region 房态管理

        public DbSet<RoomStatusSign> RoomStatusSigns { get; set; }

        #endregion

        public DbSet<Detailed> Detaileds { get; set; }

        public DbSet<CheckOut> CheckOuts { get; set; }
    }
}
